/**
 * Created by SJay on 2017/9/28.
 */
import axios from "axios"
import API from "../../api/cjy-api"
import creatUuid from "../UUID"

const getInforFun = (url, data, method = "get", header = false) => {
  var api = API()
  var url = api[url]
  var config = {
    method: method,
  }

  if (method == "get" || method == 'delete') {
    config.url = url + data
  }
  else if (method == "post") {
    config.url = url
    config.data = data
  }
  else {
    config.url = url
    config.data = data
  }

  if (header && header != 'undefined') {
    config.headers = {sessionId: header}
  }
  return axios(config);
}

export const getConfig = async () => {
  return await getInforFun('getConfig', '', 'get')

}

export const queryClassifyList = async (data) => { //获取商品分类列表的请求
  var enterpriseId = API().enterpriseId
  return await getInforFun('classList', "&token=" + new Date().getTime(), 'get')
}

export const queryNewClassifyList = async (data) => { //获取商品分类列表的请求
  var enterpriseId = API().enterpriseId
  var enterpriseCode = API().enterpriseCode
  var storeCode = localStorage.getItem('storeCode') || API().storeCode
  return await getInforFun('newClassList', `?enterpriseCode=${enterpriseCode}&storeCode=${storeCode}&token=` + new Date().getTime(), 'get')
}

export const queryNewGoodsList = async (data) => { // 获取商品详情的请求
  var api = API()
  var enterpriseCode = api.enterpriseCode
  var sessionId = localStorage.getItem('sessionId')
  var shoreCode = api.shoreCode
  data.usePlatform = 'wap'
  data.storeCode = shoreCode
  data.enterpriseCode = enterpriseCode
  return getInforFun('newGoodsList', data, 'post', sessionId)
}

export const queryGoodsList = async (data) => { // 获取商品详情的请求
  var api = API()
  var enterpriseCode = api.enterpriseCode
  var sessionId = localStorage.getItem('sessionId')
  var shoreCode = api.shoreCode
  // var usePlatform
  data.usePlatform = 'wap'
  data.storeCode = shoreCode
  data.enterpriseCode = enterpriseCode
  return getInforFun('goodsList', data, 'post', sessionId)
}

export const queryGoodsListNormal = async (data) => { // 获取商品详情的请求
  var api = API()
  var enterpriseCode = api.enterpriseCode
  var sessionId = localStorage.getItem('sessionId')
  var shoreCode = api.shoreCode
  // var usePlatform
  data.storeCode = shoreCode
  data.usePlatform = 'wap'

  data.enterpriseCode = enterpriseCode
  return getInforFun('goodsList', data, 'post', sessionId)
}

export const queryClassifyBanner = async () => {
  var enterpriseId = API().enterpriseId
  return getInforFun('queryClassifyPic', {
    enterpriseId: enterpriseId,
    pc: false
  }, 'post')
}


export const getOrder = async (data, sessionId) => {
  return getInforFun("getOrderUrl", "?token=" + new Date().getTime() + '&' + data, "get", sessionId)
}

export const querySubmitedInfor = async (data, sessionId) => {
  return getInforFun("getSubmitedInfor", "?token=" + new Date(), "get", sessionId)
}
// 获取可售后订单数量
export const getOrderCount = async (data) => {
  var sessionId = localStorage.getItem('sessionId')
  return getInforFun("getOrderCount", "?token=" + new Date().getTime(), "get", sessionId)
}

export const queryPromotionInfo = async () => {
  var api = API()
  var enterpriseId = api.enterpriseId
  var storeCode = api.storeCode
  var sessionId = localStorage.getItem('sessionId')
  return getInforFun('queryPromotionInfo',
    '?token=' + (new Date()).getTime(), 'get', sessionId)
}

//获取购物车的信息
export const queryCart = async () => {
  var enterpriseId = API().enterpriseId
  var sessionId = localStorage.getItem('sessionId')
  // var sessionUuid = sessionStorage.getItem('sessionUuid')
  /*if (!sessionUuid) {
   sessionUuid = creatUuid(22),
   sessionStorage.setItem('sessionUuid', sessionUuid)
   }*/
  return getInforFun('queryShoppingCart',
    '?token=' + (new Date()).getTime(), 'get', sessionId)
}

export const queryShoppingCartForCart = async () => {
  var api = API()
  var enterpriseId = api.enterpriseId
  var storeCode = localStorage.getItem('storeCode')
  var sessionId = localStorage.getItem('sessionId')

  return getInforFun('queryShoppingCartForCart',
    '?token=' + (new Date()).getTime() + '&storeCode=' + storeCode, 'get', sessionId)
}

// 从购物车里删除
export const deleCart = async (data) => {
  var sessionId = localStorage.getItem('sessionId')
  var enterpriseId = API().enterpriseid
  var sessionUuid = sessionStorage.getItem('sessionUuid')
  return getInforFun('deleteShoppingCart', data, 'delete', sessionId)
}


export const addCart = async (data) => {
  var sessionId = localStorage.getItem('sessionId')

  var enterpriseId = API().enterpriseid
  var sessionUuid = sessionStorage.getItem('sessionUuid')
  return getInforFun('addShoppingCart', data, 'post', sessionId)
}

export const updateCart = async (data) => {
  var sessionId = localStorage.getItem('sessionId')
  var enterpriseId = API().enterpriseid
  var sessionUuid = sessionStorage.getItem('sessionUuid')
  return getInforFun('updateShoppingCart', data, 'put', sessionId)
}


export const queryDiscount = async (data) => {
  var phone = localStorage.getItem('phone')
  return await  getInforFun('queryDiscount', 'mobile=' + data + '&token=' + new Date(), 'get',)
}


// queryRegularDetail
export const queryRegularDetail = async (data = '') => {
  var sessionid = localStorage.getItem('sessionId')
  var token = (new Date()).getTime()
  var storeCode = localStorage.getItem('storeCode') || 1616
  return getInforFun('queryRegularDetail', '?token=' + token + data + '&storeCode=' + storeCode, 'get', sessionid)
}
//queryFixedDetail


export const queryPromotionInfoOffLine = async (data) => {
  var api = API()
  var enterpriseCode = api.enterpriseCode
  var storeCode = localStorage.getItem('storeCode') || 1616
  data.enterpriseCode = enterpriseCode
  data.storeCode = storeCode
  return getInforFun('queryPromotionInfoOffLine', data, 'post')
}

export const queryFixedDetail = async (data = '') => {
  // var sessionid = localStorage.getItem('sessionId')
  var api = API()
  var enterpriseId = api.enterpriseId
  var token = (new Date()).getTime()
  return getInforFun('queryFixedDetail', '/' + data + '?token=' + token, 'get')
}

export const queryDisount = async (data) => {
  var api = API()
  var enterpriseId = api.enterpriseId
  var storeCode = api.storeCode
  var sessionId = localStorage.getItem('sessionId')
  return getInforFun('takeDiscount', '/' + data, 'get', sessionId)
}

export const queryDisountOffLine = async (data) => {
  var api = API()
  var enterpriseId = api.enterpriseId
  var storeCode = api.storeCode
  var sessionId = localStorage.getItem('sessionId')
  return getInforFun('takeDiscountOffLine', '/' + data + '/' + storeCode, 'get', sessionId)
}

export const querfinalList = async () => {
  var api = API()
  var storeCode = api.storeCode
  var url = api.goods_host + api.home_commend;
  var options = {
    method: "get",
    url,
    params: {
      enterpriseCode: api.enterpriseCode,
      storeCode,
      usePlatform: "wap"
    },
  }
  return axios(options)
}


export const scanQueryShoppingCart = async () => {
  var enterpriseId = API().enterpriseId
  var sessionId = localStorage.getItem('sessionId')
  return getInforFun('scanQueryShoppingCart',
    '?token=' + (new Date()).getTime(), 'get', sessionId)
}

export const scanQueryShoppingCartForCart = async () => {
  var api = API()
  var enterpriseId = api.enterpriseId
  var storeCode = localStorage.getItem('storeCode')
  var sessionId = localStorage.getItem('sessionId')

  return getInforFun('scanQueryShoppingCartForCart',
    '?token=' + (new Date()).getTime() + '&storeCode=' + storeCode, 'get', sessionId)
}

// 从购物车里删除
export const scanDeleteShoppingCart = async (data) => {
  var sessionId = localStorage.getItem('sessionId')
  var enterpriseId = API().enterpriseid
  var sessionUuid = sessionStorage.getItem('sessionUuid')
  return getInforFun('scanDeleteShoppingCart', data, 'delete', sessionId)
}

export const scanAddShoppingCart = async (data) => {
  var sessionId = localStorage.getItem('sessionId')

  var enterpriseId = API().enterpriseid
  var sessionUuid = sessionStorage.getItem('sessionUuid')
  return getInforFun('scanAddShoppingCart', data, 'post', sessionId)
}


export const scanUpdateShoppingCart = async (data) => {
  var sessionId = localStorage.getItem('sessionId')
  var enterpriseId = API().enterpriseid
  var sessionUuid = sessionStorage.getItem('sessionUuid')
  return getInforFun('scanUpdateShoppingCart', data, 'put', sessionId)
}

export const scanQueryPromotionInfo = async () => {
  var api = API()
  var enterpriseId = api.enterpriseId
  var storeCode = api.storeCode
  var sessionId = localStorage.getItem('sessionId')
  return getInforFun('scanQueryPromotionInfo',
    '?token=' + (new Date()).getTime(), 'get', sessionId)
}

export const selectAfterSalesPageList = async (data, sessionId) => {

  return getInforFun("selectAfterSalesPageList", "?" + data + "&token=" + new Date().getTime(), "get", sessionId)

}


export const queryProcessList = async (data, sessionId) => {
  var api = API()
  var enterpriseId = api.enterpriseId
  return getInforFun("queryProcessList", `?enterpriseid=${enterpriseId}&goodsid=${data}&token=${new Date().getTime()}`, "get", sessionId)

}
